package com.edu.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.edu.dao.BaseDao;
import com.edu.dao.UserDao;
import com.edu.dao.Impl.UserDaoImpl;
import com.edu.entity.User;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");//设置响应编码
		response.setCharacterEncoding("UTF-8");//设置请求编码
		
		response.setContentType("text/html;charset = UTF-8");//设置响应类型
		
		String username = request.getParameter("username");//获取用户名
		String userpass = request.getParameter("userpass");//获取密码
		
		UserDao userDaoImpl = new UserDaoImpl();//创建UserDaoImpl对象
		User loging = userDaoImpl.Login(username, userpass);//调用Login（）方法进行登录校验，并返回User对象
		System.out.println(loging);
		
		if (loging != null) {//如果登录成功，则将User对象存储到Session中，并重定向到Servlet
			HttpSession session = request.getSession();
			session.setAttribute("user",loging);
			request.getRequestDispatcher("index.jsp").forward(request, response);
			
		}else {

            request.setAttribute("mes","用户名或密码错误！");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
		}
	}

}
